package org.magento.pages;

import org.magento.data.CustomerData;
import org.magento.data.NewCustomerData;
import org.magento.utils.ConfigProperties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

//This class presents /admin/customer page
public class ManageCustomers extends Page {
	
	//Locators:
	@FindBy (xpath = "/descendant::td[@class='filter-actions a-right']/button[@title='Reset Filter']")
	public WebElement resetFilterButton;
	
	@FindBy (id = "customerGrid_filter_email")
	public WebElement emailSearchField;
	
	@FindBy (xpath = "/descendant::td[@class='filter-actions a-right']/button[@title='Search']")
	public WebElement searchButton;
	
	@FindBy (linkText = "Edit")
	public WebElement customerEditButton;
	
	//Actions 
	public EditCustomerBackEnd editCustomer(NewCustomerData newCustomer) throws Exception {
		resetFilterButton.click();
		waitForFiveSeconds();
		type(emailSearchField, newCustomer.email);
		waitForFiveSeconds();
		searchButton.click();
		waitForFiveSeconds();
		customerEditButton.click();
		return PageFactory.initElements(driver, EditCustomerBackEnd.class);
	}
	
	public EditCustomerBackEnd editCustomer(CustomerData customer) throws Exception {
		resetFilterButton.click();
		waitForFiveSeconds();
		type(emailSearchField, customer.email);
		waitForFiveSeconds();
		searchButton.click();
		waitForFiveSeconds();
		customerEditButton.click();
		return PageFactory.initElements(driver, EditCustomerBackEnd.class);
	}
	
	//Assertions: 
	public boolean isNewCustomerDeleted (NewCustomerData newCustomer) throws Exception {
		resetFilterButton.click();
		type(emailSearchField, newCustomer.email);
		searchButton.click();
		waitForFiveSeconds();
		return driver.findElement(By.cssSelector("BODY")).getText().contains("No records found.");
	}
	
	public boolean isManageCustomersPageIsDisplayed() {
		return driver.findElement(By.cssSelector("BODY")).getText().contains("Manage Customers");
	}
	
	public boolean isCustomerDeleted() {
		return driver.findElement(By.cssSelector("BODY")).getText().contains("The customer has been deleted.");
	}

	//Auto - generated
	public ManageCustomers(WebDriver driver) {
		super(driver);
	}

	@Override
	public void open() {
		driver.get(ConfigProperties.getProperty("url.manageCustomers"));	 		
	}

}
